package com.qs.zk.utils;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * curator客户端工具类，用来获取CuratorFramework单例
 *
 * @author qinyupeng
 * @since 2019-03-15 09:50:55
 */
public class CuratorClientUtils {

    private static final String connectString = "172.16.109.130:2181,172.16.109.131:2181,172.16.109.132:2181";
    private static volatile CuratorFramework instance;

    private CuratorClientUtils() {

    }

    public static CuratorFramework getInstance() {
        if (instance == null) {
            synchronized (CuratorClientUtils.class) {
                instance = CuratorFrameworkFactory.builder()
                        .connectString(connectString)
                        .sessionTimeoutMs(5000)
                        .connectionTimeoutMs(10000)
                        .retryPolicy(new ExponentialBackoffRetry(2000, 3))
                        .build();
                instance.start();
            }
        }

        return instance;
    }


    public static void main(String[] args) {
        CuratorFramework instance = CuratorClientUtils.getInstance();
        CuratorFramework instance2 = CuratorClientUtils.getInstance();
        System.out.println("是否相同实例？" + (instance == instance2));
        System.out.println("---->>>instance=" + instance);
    }

}
